package com.example.service;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.Mapper.LF_InventoryMapper;
import com.example.Mapper.LF_PandiancongMapper;
import com.example.pojo.LF_Inventory;
import com.example.pojo.LF_Pandiancong;
import com.example.pojo.Materialzhu;
import com.example.pojo.Page;

@Service
public class LF_InventoryService {
	@Autowired
    private LF_InventoryMapper invMapper;
	@Autowired
	private LF_PandiancongMapper pandianMapper;
	public Page findAll(int currentPage){
		//查询总记录数
		int count=this.invMapper.findCount();
		System.out.println(count);
		//根据当前页查询数据
		LF_Inventory inv=this.invMapper.findAll(currentPage-1);		
		//根据单号查询明细表	
	    //System.out.println(this.invMapper.findDetail(inv.getBillno()));
	    inv.setDetail(this.invMapper.findDetail(inv.getBillno()));		
		Page page=new Page(currentPage,1,count,inv);
		System.out.println(page+"===="+inv.getBillno());	
		return page;
	}
	/*public List<LF_Inventory> findAll(){
		List<LF_Inventory> inv=this.invMapper.findAll();
		//根据单号查询明细表
		return inv;
	}*/
	public void insertPandian(LF_Inventory inv){
		 List<LF_Inventory> list=this.invMapper.selectBillno(inv.getBillno());
		 if(list.size()>0){
			//删除明细表
			 this.pandianMapper.deletePandianX(inv.getBillno());
			 //单据存在时则修改
			 this.invMapper.updatePandian(inv);
		 }else{
			 //单据不存在则新增主表
			 this.invMapper.insertPandian(inv);
		 }
		
		 for (LF_Pandiancong details : inv.getDetail()) {
			 details.setBillno(inv.getBillno());
		    //添加明细表
			this.pandianMapper.insertPandianX(details);
		}
	}
	
	public void deletePandian(String Billno){
		//删除明细表8
		 this.pandianMapper.deletePandianX(Billno);
		//删除主表
		this.invMapper.deletePandian(Billno);
	}
	
	public String findDate(String AdjustDate){
		AdjustDate=AdjustDate.replaceAll("-", "");
		String Billno=this.invMapper.findDate(AdjustDate);
		if(Billno==null){
			return AdjustDate+"01";			
		}
		return (Long.parseLong(Billno)+1)+"";
	}
	
	public List<Materialzhu> findWu(int wareId){
		
		return this.invMapper.findWu(wareId);
	}
}
